<template>
  <div class="course-list">
    <el-card>
      <template #header>
        <span>数据筛选</span>
      </template>
      <el-form
        ref="searchComponent"
        label-width="70px"
        label-position="left"
        :model="filterParams"
      >
        <el-form-item
          label="课程名称"
          prop="courseName"
        >
          <el-input v-model="filterParams.courseName" />
        </el-form-item>
        <el-form-item
          label="状态"
          prop="status"
        >
          <el-select v-model="filterParams.status">
            <el-option
              label="全部"
              value=""
            />
            <el-option
              label="上架"
              value="1"
            />
            <el-option
              label="下架"
              value="0"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            :disabled="loading"
            @click="handleReset"
          >
            重置
          </el-button>
          <el-button
            type="primary"
            :disabled="loading"
            @click="handleFilter"
          >
            查询
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card>
      <template #header>
        <span>查询结果：</span>
        <el-button
          style="float: right; margin-top: -5px"
          type="primary"
          @click="$router.push({
            name: 'course-create'
          })"
        >
          添加课程
        </el-button>
      </template>
      <el-table
        v-loading="loading"
        :data="courses"
        style="width: 100%; margin-bottom: 20px"
      >
        <el-table-column
          prop="id"
          label="ID"
          width="50"
        />
        <el-table-column
          prop="courseName"
          label="课程名称"
          width="120"
        />
        <el-table-column
          prop="price"
          label="价格"
        />
        <el-table-column
          prop="sortNum"
          label="排序"
        />
        <el-table-column
          prop="status"
          label="上架状态"
        >
          <template #default="scope">
            <el-switch
              v-model="scope.row.status"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
              :disabled="scope.row.isStatusLoading"
              @change="onStateChange(scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column
          prop="price"
          label="操作"
          width="180"
          align="center"
        >
          <template #default="scope">
            <el-button
              type="text"
              @click="$router.push({
                name: 'course-edit',
                params: {
                  courseId: scope.row.id
                }
              })"
            >
              编辑
            </el-button>
            <el-button
              type="text"
              @click="$router.push({
                name: 'course-section',
                params: {
                  courseId: scope.row.id
                }
              })"
            >
              内容管理
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        layout="prev, pager, next"
        :total="totalCount"
        :disabled="loading"
        @current-change="handleCurrentChange"
      />
    </el-card>
  </div>
</template>

<script lang="ts">
import { ElMessage } from 'element-plus'
import { DefineComponent, defineComponent, onBeforeMount, reactive, ref } from 'vue'
import { getQueryCourses, changeState } from '/@/services/course'
interface FilterParams {
  currentPage: number
  pageSize: number
  courseName: string
  status: string
}
const searchComponent = ref<DefineComponent|null>(null)
let filterParams: FilterParams
const useInit = () => {
  filterParams = reactive({
    currentPage: 1,
    pageSize: 10,
    courseName: '',
    status: ''
  })
  const loading = ref(false)
  const courses = ref([])
  const totalCount = ref(0)
  const loadCourses = async () => {
    const { data } = await getQueryCourses(filterParams)
    data.data.records.forEach((item: any) => {
      item.isStatusLoading = false
    })
    courses.value = data.data.records
    totalCount.value = data.data.total
    loading.value = false
  }
  onBeforeMount(loadCourses)
  return {
    filterParams,
    loading,
    courses,
    totalCount,
    loadCourses
  }
}
const useHandles = (loadDataFn: () => void) => {
  const handleCurrentChange = (page: number) => {
    filterParams.currentPage = page
    loadDataFn()
  }

  const handleFilter = () => {
    filterParams.currentPage = 1
    loadDataFn()
  }

  const handleReset = () => {
    (searchComponent.value as DefineComponent).resetFields()
    filterParams.currentPage = 1
    loadDataFn()
  }
  const onStateChange = async (course: any) => {
    if (!course.id) return
    course.isStatusLoading = true
    const { data } = await changeState({
      courseId: course.id,
      status: course.status
    })
    data.code === '000000'
      ? ElMessage.success(`${course.status === 0 ? '下架' : '上架'}成功`)
      : ElMessage.error(`${course.status === 0 ? '下架' : '上架'}失败`)
    course.isStatusLoading = false
  }
  return {
    onStateChange,
    handleCurrentChange,
    handleFilter,
    handleReset
  }
}
export default defineComponent({
  name: 'CourseList',
  setup () {
    const initObj = useInit()
    const { loadCourses } = initObj
    return {
      searchComponent,
      ...initObj,
      ...useHandles(loadCourses)
    }
  }
})
</script>

<style lang="scss" scoped>
.el-card {
  margin-bottom: 20px;
}
</style>
